// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
	async editAction(){
		console.log(this.post())
		if(this.isPost){
			//判断是否有重复
			let is_exist = await this.model('or_admin').where({id:['!=',this.post('id')],username:this.post('username')}).find();
			if(!think.isEmpty(is_exist)){
				return this.fail('此用户名已经存在！');
			}
			let res = await this.model("or_admin").where({id:this.post('id')}).update(this.post());
			if(res){
				await this.update_session();
				return this.success({info:"编辑成功！"});
			}else{
				return this.fail('编辑失败！');	
			}
		}
		return this.display();
	}
	
	async changepasAction(){
		if(this.isPost){
			let oldpassword = think.md5(this.post('oldpassword'));
			let password = think.md5(this.post('password'));
			let rpassword = think.md5(this.post('rpassword'));
			
			if(password !== rpassword){
				return this.fail('两次输入的密码不一致！');
			}
			
			//判断旧密码是否正确
			if(oldpassword !== this.shop_userinfo.password){
				return this.fail('旧密码填写错误！');
			}
			let res = await this.model("or_admin").where({id:this.shop_userinfo.id}).update({password:password});
			if(res){
				await this.update_session();
				return this.success({info:"修改成功！"});
			}else{
				return this.fail('修改失败！');	
			}
		}else{
			return this.display();
		}	
	}
	
	async adminlistAction(){
		if(this.isPost){
			let alist = await this.model('or_shop_admin')
						.alias('sa')
						.field('sa.id,sa.username,sa.nickname,og.name,og.code,sa.phone')
						.join("think_organization og ON sa.o_id = og.id")
						.where({s_id:this.shop_userinfo.s_id})
						.select();
						
			let relist = {
				"draw": '',
				"data": alist,
				"breadcrumb":''
			}
			return this.json(relist);
		}
		this.meta_title = '管理员列表';
		return this.display(); 
	}
	
	async addadminAction(){
		if(this.isPost){
			let data = this.post();
			data.password = think.md5(data.password);
			data.o_id = this.shop_userinfo.o_id;
			data.o_code = this.shop_userinfo.o_code;
			data.s_id = this.shop_userinfo.s_id;
			//判断是否有重复
			let is_exist = await this.model('or_shop_admin').where({username:data.username}).find();
			if(!think.isEmpty(is_exist)){
				return this.fail('此账号已存在！');
			}
			let res = await this.model('or_shop_admin').add(data);
			return res?this.success({info:"添加成功！"}):this.fail('添加失败！');
		}
		return this.display();
	}
	
	async editadminAction(){
		if(this.isPost){
			let data = this.post();
			let is_exist = await this.model('or_shop_admin').where({username:data.username,id: ["!=",data.id]}).find();
			if(!think.isEmpty(is_exist)){
				return this.fail('该登陆账号已经存在！');
			}
			data['password']?data['password'] = think.md5(data['password']):delete data['password'];
			let res = await this.model('or_shop_admin').where({id:data.id}).update(data);
			//如果是当前管理员更新session
			if(res && data.id == this.shop_userinfo.id){
				await this.update_session();
			}
			return res?this.success({info:"编辑成功！"}):this.fail('编辑失败！');
		}
		let id = this.get('id');
		let ainfo = await this.model('or_shop_admin').where({id:id}).find();
		return this.json(ainfo);
	}
	
	async deleteadminAction(){
		let id = this.post('id');
		let res = await this.model('or_shop_admin').where({id:id}).delete();
		return res?this.success({info:"删除成功！"}):this.fail('删除失败！');
	}
}